<template>
  <div>
    <div class="header">
      <router-link to="/">义齿家交易平台</router-link>
    </div>

    <div class="content">
      <div class="center">
        <h1>用户注册</h1>
        <el-divider />
        <div style="width:70%;margin:0px auto">
          <el-steps :active="active" finish-status="success">
            <el-step title="注册" />
            <el-step title="完善信息" />
            <el-step title="完成" />
          </el-steps>
        </div>
        <!-- 注册信息 -->
        <el-form v-if="active==0" ref="ruleForm" :model="user" :rules="rules" style="width:60%; margin:0px auto" label-width="90px">
          <el-form-item label="用户名" prop="name">
            <el-input v-model="user.name" />
          </el-form-item>
          <el-form-item label="手机号" prop="phone">
            <el-input v-model="user.phone" />
          </el-form-item>
          <el-form-item label="密码" prop="loginPwd">
            <el-input v-model="user.loginPwd" show-password />
          </el-form-item>
          <el-form-item label="确认密码" prop="comfirmPwd">
            <el-input v-model="comfirmPwd" show-password />
          </el-form-item>
          <el-form-item label="类型" prop="type">
            <el-switch
              v-model="type.worker"
              active-color="#13ce66"
              inactive-color="#ccc"
              active-text="我有任务"
              active-value="2"
              inactive-value="1"
            />
            <el-switch
              v-model="type.boss"
              active-color="#13ce66"
              inactive-color="#ccc"
              active-text="我会技能"
              style="margin:0px 30px"
              active-value="2"
              inactive-value="1"
            />
          </el-form-item>

          <el-form-item label="验证码" prop="validateCode">
            <el-input v-model="user.validateCode" />
          </el-form-item>

          <el-form-item>
            <el-button type="primary" @click="submitForm('ruleForm')">注册</el-button>
          </el-form-item>
        </el-form>
        <!-- 基本信息 -->
        <el-form v-if="active==1" ref="infoForm" :model="baseInfo" :rules="rules" style="width:60%; margin:0px auto" label-width="90px">
          <el-row :gutter="20">
            <el-col :span="8" style="text-align:center">
              <el-upload
                class="avatar-uploader"
                :action="baseUrl+'api/Files'"
                :show-file-list="false"
                :on-success="handleAvatarSuccess"
                :before-upload="beforeAvatarUpload"
              >
                <img v-if="user.image" :src="baseUrl+'files/'+imageUrl" class="avatar">
                <div v-else class="avatar">
                  <i class="el-icon-plus avatar-uploader-icon" />头像
                </div>

              </el-upload>
            </el-col>
            <el-col :span="16">
              <el-form-item label="性别" prop="sex">
                <el-input v-model="baseInfo.sex" />
              </el-form-item>
              <el-form-item label="年龄" prop="age">
                <el-input v-model="baseInfo.age" />
              </el-form-item>
              <el-form-item label="地址" prop="address">
                <el-input v-model="baseInfo.address" />
              </el-form-item>
              <el-form-item label="简介" prop="description">
                <el-input
                  v-model="baseInfo.description"
                  type="textarea"
                  :rows="2"
                  placeholder="请输入内容"
                />
              </el-form-item>
              <el-form-item>
                <el-button type="primary" @click="postInfo('infoForm')">下一步</el-button>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <div v-if="active==2" style="width:60%; margin:0px auto">
          <be-good-at userid="72d747e0-a791-45b3-bc96-421f3ba8fb1f" />
          <div style="text-align:center;padding:5px">
            <router-link to="/login" tag="button" class="el-button el-button--primary">设置完成，去登陆</router-link>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import { register, setPhoto, edit } from '@/api/user'
import beGoodAt from '@/views/personal/beGoodAt.vue'
export default {
  components: { beGoodAt },

  data() {
    return {
      active: 0, // 步骤条状态
      type: { // 类型开关  1：没有  2： 有
        worker: '1',
        boss: '2'
      },
      comfirmPwd: '',
      currUserId: null, // 注册之后的Id

      imageUrl: '', // 头像地址
      baseUrl: process.env.VUE_APP_BASE_API,

      user: {
        'name': '',
        'loginPwd': '',
        'phone': '',
        'sex': '男',
        'image': '',
        'address': '',
        'userType': this.userType,
        'validateCode': 'string',
        'products': [],
        'tools': [],
        'toolName': null
      },
      baseInfo: {
        name: null,
        sex: '男',
        address: '',
        age: 20,
        description: ''
      },
      rules: {
        name: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        phone: [
          { required: true, message: '请输入手机号', trigger: 'blur' }
        ],
        loginPwd: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {
    userType() {
      return parseInt(this.type.worker + this.type.boss)
    }
  },
  created() {
    this.imageUrl = ''
  },
  methods: {

    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.user.userType = this.userType

          register(this.user).then(res => {
            this.currUserId = res.data.id // 记录返回的id
            this.$message({
              message: '恭喜你，注册完成！请完善资料',
              type: 'success'
            })
            this.active++
          })
            .catch(err => {
              console.log(err)
            })
        } else {
          return false
        }
      })
    },
    postInfo(formName) { // 提交基本信息
      this.$refs[formName].validate((valid) => {
        if (valid) {
          edit(this.currUserId, this.baseInfo).then(res => {
            if (this.userType.toString().charAt(0) === 1) {
              this.active = 2
              this.$message({
                message: '资料补充完成，前往登录！',
                type: 'success'
              })
              this.$router.push('/login')
            } else {
              this.active++
            }
          })
            .catch(err => {
              console.log(err)
            })
        } else {
          return false
        }
      })
    },

    // 上传头像
    handleAvatarSuccess(res, file) {
      setPhoto(this.currUserId, res.data.path).then(response => {
        this.$message('设置完成！')
        this.imageUrl = res.data.path
      }).catch(err => {
        console.log(err)
      })
    },
    // 上传之前
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg'
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isJPG && isLt2M
    }

  }

}
</script>
<style  scoped>
    .header{
        background: #000;
        color: #fff;
        font-weight: 900;
        font-size: 26px;
        line-height: 60px;
        padding-left: 100px;
    }

    .content{

        background-color: rgb(248, 63, 7);
        padding: 30px 100px;
    }
    .center{
        background-color: #fff;
        /* margin: 40px 100px; */
        border-radius: 5px;
        box-shadow: 0 0 3px #fff;
        padding: 10px;
    }

    .center h1{
      text-align: center;
      letter-spacing: 10px;
      color: #666;
      margin: 0px;
    }

    .avatar{
      width:200px;
      height: 200px;
      line-height: 200px;
      border: 3px solid #ccc;
      border-radius: 5px;
      text-align: center;
    }
</style>
